
const app = getApp()

var timer
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isScan:null,
    player:0,
    money:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    let that = this
    if(options && options.isScan){
        this.setData({
          isScan:options.isScan
        })
    }
    if(options && options.id && options.type){
        this.setData({
          id:parseInt(options.id),
          type:parseInt(options.type)
        })
        this.getOrderDetail()
    }else{

    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getOrderDetail:function(e){
    var that=this
    var id=this.data.id
    var data={}
    data.id=id
    var type=this.data.type
    var url=''
    if(type==1){
      url='ht/store/userQrOrderDetails'
    }else if(type==2 || type==4){
      url='ht/store/userOrderDetails'
    }else{
      wx.showToast({
        title: '参数错误',
        icon:'none',
        duration:1500
      })
      return false
    }

    app.agriknow.getOrderDetail(data,url).then(res=>{
      var datas=res.data
      that.setData({
        detail:datas
      })
      if(datas.surplusTime>0){
       
        //that.countDown(datas.surplusTime)
      }
    })
  },
  toCheckQrcode:function(){
    var that=this
    var isScan=this.data.isScan
    if(!isScan){
      return false
    }
    wx.showModal({
      title: '温馨提示',
      content: '是否确认核销该订单',
      success: function (res) {
        if (res.confirm) {
            that.checkQrcode()
        } else if (res.cancel) {
          console.log('取消删除')
        }
      }
    })
  },
  checkQrcode:function(){
    var that=this
    var detail=this.data.detail
    var data={}
    data.orderId=detail.id
    data.storeId=wx.getStorageSync('userInfo').storeId
    app.agriknow.checkQrcode(data).then(res=>{
      if(res.code=='000000'){
        wx.showToast({
          title: '核验成功',
          icon:'success',
          duration:1500,
        })
        setTimeout(function(){
          that.getOrderDetail()
        },1500)
      }else{
        wx.showToast({
          title: res.data,
          icon:'success',
          duration:1500,
        })
      }
    })
  },
  openLocation:function(){
    var detail=this.data.detail
    wx.openLocation({
      latitude:detail.latitude,
      longitude:detail.situation,
      name:detail.souceStoreName,
      address:detail.address,
      scale: 18,
      fail:function(err){
        console.log(err)
      }
    })
  },
  call:function(e){
    var mobile=e.currentTarget.dataset.mobile
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  close:function(e){
    this.setData({
      player:0,
      money:0
    })
  },
  toRefund:function(){
    /*
    this.setData({
      player:1

    })
    */
   wx.navigateTo({
     url: '/pages/orderDetail/refund?id='+this.data.id,
   })
  },
  input:function(e){
    var key=e.currentTarget.dataset.key
    this.setData({
      [key]:e.detail.value
    })
  },
  refund:function(e){
    var that=this
    var detail=this.data.detail
    var money=this.data.money
    var data={}
    var tips=''
    if(money<=0){
        tips='请输入退款金额'
    }else if(money>detail.price){
       tips='退款金额不能超出订单金额'
    }
    if(tips){
      wx.showToast({
        title: tips,
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }

    data.orderId=detail.id
    data.price=money
    wx.showModal({
      title: '温馨提示',
      content: '是否确认给客户退款',
      success: function (res) {
        if (res.confirm) {
          app.agriknow.commRequest('ht/store/refundOrderAuditing',data).then(res=>{
            console.log(res)
            that.setData({
              player:0,
              money:0
            })
            wx.navigateBack()
          })
        } else if (res.cancel) {
          console.log('取消删除')
        }
      }
    })
    
  },
  

})